package com.maserb.monitor.web.jw_eduction;


import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.JwEducationClobEntity;
import com.maserb.monitor.entity.JwEducationInfoEntity;
import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.enums.EducationInfoTypeEnum;
import com.maserb.monitor.enums.JwOrgTypeEnum;
import com.maserb.monitor.model.JwOrgTreeModel;
import com.maserb.monitor.service.base.IToolService;
import com.maserb.monitor.service.jwEducation.JwEducationInfoService;
import com.maserb.monitor.service.jwEducation.JwOrgService;
import com.maserb.monitor.service.remote.EcCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/jwOrg")
public class JwOrgController {
    @Autowired
    private JwEducationInfoService service;

    @Autowired
    private JwOrgService orgService;

    @Autowired
    private IToolService toolService;

    @Autowired
    private EcCardService ecCardService;

    @RequestMapping(value = {"/", "/index"})
    public String getIndex() {
        return "jw_education/orgList";
    }

    @RequestMapping("/doListOrg")
    @ResponseBody
    public PageResponseResult<JwOrgEntity> doListOrg(PageRequestParam page, final String name) {
        return AjaxUtils.processPage(page, new Func_T1<Pageable, Page<JwOrgEntity>>() {
            @Override
            public Page<JwOrgEntity> invoke(Pageable var1) throws Exception {
                Page<JwOrgEntity> page = orgService.findAll(var1, name);
                for (JwOrgEntity entity : page.getContent()) {
                    entity.setCreateTime(toolService.formatYYMMDDHHMMSS(entity.getCreateTime()));
                    if (null != entity.getType() && !"".equals(entity.getType())) {
                        entity.setType(JwOrgTypeEnum.getNameByValue(Integer.parseInt(entity.getType())));
                    }
                }
                return page;
            }
        });
    }

    //三级机构数
    @RequestMapping("/doListTreeOrg")
    @ResponseBody
    public List<JwOrgTreeModel> doListTreeOrg() {
        List<JwOrgEntity> orgs = orgService.findByTreeLevel(0);
        List<JwOrgTreeModel> modelList = new ArrayList<JwOrgTreeModel>();
        for(JwOrgEntity jwOrgEntity : orgs){
            JwOrgTreeModel orgTreeModel = new JwOrgTreeModel();
            orgTreeModel.setName(jwOrgEntity.getName());
            orgTreeModel.setId(jwOrgEntity.getId());
            orgTreeModel.setText(jwOrgEntity.getName());
            orgTreeModel.setTreeLevel(jwOrgEntity.getTreeLevel());
            orgTreeModel.setDescription(jwOrgEntity.getDescription());
            if (null != jwOrgEntity.getType() && !"".equals(jwOrgEntity.getType())) {
                orgTreeModel.setType(JwOrgTypeEnum.getNameByValue(Integer.parseInt(jwOrgEntity.getType())));
            }
            orgTreeModel.setState("open");
            List<JwOrgTreeModel> modelList2 = new ArrayList<JwOrgTreeModel>();
            for(JwOrgEntity orgEntity:jwOrgEntity.getOrgChild()){
                JwOrgTreeModel orgTreeModel2 = new JwOrgTreeModel();
                orgTreeModel2.setName(orgEntity.getName());
                orgTreeModel2.setId(orgEntity.getId());
                orgTreeModel2.setText(orgEntity.getName());
                orgTreeModel2.setTreeLevel(orgEntity.getTreeLevel());
                orgTreeModel2.setParentId(orgEntity.getParent().getId());
                orgTreeModel2.setDescription(orgEntity.getDescription());
                if (null != orgEntity.getType() && !"".equals(orgEntity.getType())) {
                    orgTreeModel2.setType(JwOrgTypeEnum.getNameByValue(Integer.parseInt(orgEntity.getType())));
                }
                List<JwOrgTreeModel> modelList3 = new ArrayList<JwOrgTreeModel>();
                for(JwOrgEntity orgEntity2:orgEntity.getOrgChild()){
                    JwOrgTreeModel orgTreeModel3 = new JwOrgTreeModel();
                    orgTreeModel3.setName(orgEntity2.getName());
                    orgTreeModel3.setId(orgEntity2.getId());
                    orgTreeModel3.setText(orgEntity2.getName());
                    orgTreeModel3.setTreeLevel(orgEntity2.getTreeLevel());
                    orgTreeModel3.setParentId(orgEntity2.getParent().getId());
                    orgTreeModel3.setDescription(orgEntity2.getDescription());
                    if (null != orgEntity2.getType() && !"".equals(orgEntity2.getType())) {
                        orgTreeModel3.setType(JwOrgTypeEnum.getNameByValue(Integer.parseInt(orgEntity2.getType())));
                    }
                    modelList3.add(orgTreeModel3);
                }
                if(null!=modelList3 && modelList3.size()>0){
                    orgTreeModel2.setState("closed");
                }
                orgTreeModel2.setChildren(modelList3);
                modelList2.add(orgTreeModel2);
            }
            orgTreeModel.setChildren(modelList2);
            modelList.add(orgTreeModel);
        }
        return modelList;
    }

    //二级机构树
    @RequestMapping("/doListTreeOrgParent")
    @ResponseBody
    public List<JwOrgTreeModel> doListTreeOrgParent() {
        List<JwOrgEntity> orgs = orgService.findByTreeLevel(0);
        List<JwOrgTreeModel> modelList = new ArrayList<JwOrgTreeModel>();
        for(JwOrgEntity jwOrgEntity : orgs){
            JwOrgTreeModel orgTreeModel = new JwOrgTreeModel();
            orgTreeModel.setName(jwOrgEntity.getName());
            orgTreeModel.setId(jwOrgEntity.getId());
            orgTreeModel.setText(jwOrgEntity.getName());
            orgTreeModel.setTreeLevel(jwOrgEntity.getTreeLevel());
            orgTreeModel.setDescription(jwOrgEntity.getDescription());
            orgTreeModel.setType(jwOrgEntity.getType());
            orgTreeModel.setState("open");
            List<JwOrgTreeModel> modelList2 = new ArrayList<JwOrgTreeModel>();
            for(JwOrgEntity orgEntity:jwOrgEntity.getOrgChild()){
                JwOrgTreeModel orgTreeModel2 = new JwOrgTreeModel();
                orgTreeModel2.setName(orgEntity.getName());
                orgTreeModel2.setId(orgEntity.getId());
                orgTreeModel2.setText(orgEntity.getName());
                orgTreeModel2.setTreeLevel(orgEntity.getTreeLevel());
                orgTreeModel2.setParentId(orgEntity.getParent().getId());
                orgTreeModel2.setDescription(orgEntity.getDescription());
                orgTreeModel2.setType(orgEntity.getType());
                modelList2.add(orgTreeModel2);
            }
            orgTreeModel.setChildren(modelList2);
            modelList.add(orgTreeModel);
        }
        return modelList;
    }

    //一级机构树
    @RequestMapping("/doListTreeOrgLevel")
    @ResponseBody
    public List<JwOrgTreeModel> doListTreeOrgLevel() {
        List<JwOrgEntity> orgs = orgService.findByTreeLevel(0);
        List<JwOrgTreeModel> modelList = new ArrayList<JwOrgTreeModel>();
        for(JwOrgEntity jwOrgEntity : orgs){
            JwOrgTreeModel orgTreeModel = new JwOrgTreeModel();
            orgTreeModel.setName(jwOrgEntity.getName());
            orgTreeModel.setId(jwOrgEntity.getId());
            orgTreeModel.setText(jwOrgEntity.getName());
            orgTreeModel.setTreeLevel(jwOrgEntity.getTreeLevel());
            orgTreeModel.setDescription(jwOrgEntity.getDescription());
            orgTreeModel.setType(jwOrgEntity.getType());
            modelList.add(orgTreeModel);
        }
        return modelList;
    }

    /**
     * 根据登录用户返回用户学校及其下级机构树
     * @param request
     * @return
     */
    @RequestMapping("/doListTreeOrgByUser")
    @ResponseBody
    public List<JwOrgTreeModel> doListTreeOrgByUser(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        UserEntity user = (UserEntity)session.getAttribute("user");
        JwOrgEntity jwOrgEntity = orgService.findOne(user.getOrgId());
        List<JwOrgTreeModel> modelList = new ArrayList<JwOrgTreeModel>();
        if(jwOrgEntity.getTreeLevel()==0){
            modelList=doListTreeOrg();
        }else if(jwOrgEntity.getTreeLevel()==1){
            JwOrgTreeModel orgTreeModel = new JwOrgTreeModel();
            orgTreeModel.setName(jwOrgEntity.getName());
            orgTreeModel.setId(jwOrgEntity.getId());
            orgTreeModel.setText(jwOrgEntity.getName());
            orgTreeModel.setTreeLevel(jwOrgEntity.getTreeLevel());
            orgTreeModel.setDescription(jwOrgEntity.getDescription());
            orgTreeModel.setType(jwOrgEntity.getType());
            orgTreeModel.setState("open");
            List<JwOrgTreeModel> modelList2 = new ArrayList<JwOrgTreeModel>();
            for(JwOrgEntity orgEntity:jwOrgEntity.getOrgChild()){
                JwOrgTreeModel orgTreeModel2 = new JwOrgTreeModel();
                orgTreeModel2.setName(orgEntity.getName());
                orgTreeModel2.setId(orgEntity.getId());
                orgTreeModel2.setText(orgEntity.getName());
                orgTreeModel2.setTreeLevel(orgEntity.getTreeLevel());
                orgTreeModel2.setParentId(orgEntity.getParent().getId());
                orgTreeModel2.setDescription(orgEntity.getDescription());
                orgTreeModel2.setType(orgEntity.getType());
                modelList2.add(orgTreeModel2);
            }
            orgTreeModel.setChildren(modelList2);
            modelList.add(orgTreeModel);
        }
        return modelList;
    }

    /**
     * 根据登录用户返回用户管理的班级树
     * @param request
     * @return
     */
    @RequestMapping("/doClazzListTreeOrgByUser")
    @ResponseBody
    public List<JwOrgTreeModel> doClazzListTreeOrgByUser(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        UserEntity user = (UserEntity)session.getAttribute("user");
        JwOrgEntity jwOrgEntity = orgService.findOne(user.getOrgId());
        List<JwOrgTreeModel> modelList = new ArrayList<JwOrgTreeModel>();

        List<JwOrgEntity>clazzList = orgService.findByUser(user);
        if(null!=clazzList && clazzList.size()>0){
            for(JwOrgEntity jwOrg:clazzList ){
                JwOrgTreeModel orgTreeModelClazz = new JwOrgTreeModel();
                orgTreeModelClazz.setName(jwOrg.getName());
                orgTreeModelClazz.setId(jwOrg.getId());
                orgTreeModelClazz.setText(jwOrg.getName());
                orgTreeModelClazz.setTreeLevel(jwOrg.getTreeLevel());
                orgTreeModelClazz.setParentId(jwOrg.getParent().getId());
                orgTreeModelClazz.setDescription(jwOrg.getDescription());
                orgTreeModelClazz.setType(jwOrg.getType());
                modelList.add(orgTreeModelClazz);
            }
            return modelList;
        }
        if(jwOrgEntity.getTreeLevel()==0){
            modelList=doListTreeOrg();
        }else if(jwOrgEntity.getTreeLevel()==1){
            JwOrgTreeModel orgTreeModel = new JwOrgTreeModel();
            orgTreeModel.setName(jwOrgEntity.getName());
            orgTreeModel.setId(jwOrgEntity.getId());
            orgTreeModel.setText(jwOrgEntity.getName());
            orgTreeModel.setTreeLevel(jwOrgEntity.getTreeLevel());
            orgTreeModel.setDescription(jwOrgEntity.getDescription());
            orgTreeModel.setType(jwOrgEntity.getType());
            orgTreeModel.setState("open");
            List<JwOrgTreeModel> modelList2 = new ArrayList<JwOrgTreeModel>();
            for(JwOrgEntity orgEntity:jwOrgEntity.getOrgChild()){
                JwOrgTreeModel orgTreeModel2 = new JwOrgTreeModel();
                orgTreeModel2.setName(orgEntity.getName());
                orgTreeModel2.setId(orgEntity.getId());
                orgTreeModel2.setText(orgEntity.getName());
                orgTreeModel2.setTreeLevel(orgEntity.getTreeLevel());
                orgTreeModel2.setParentId(orgEntity.getParent().getId());
                orgTreeModel2.setDescription(orgEntity.getDescription());
                orgTreeModel2.setType(orgEntity.getType());
                modelList2.add(orgTreeModel2);
            }
            orgTreeModel.setChildren(modelList2);
            modelList.add(orgTreeModel);
        }
        return modelList;
    }

    /**
     * 根据登录用户返回用户学校及其管理的班级树
     * @param request
     * @return
     */
    @RequestMapping("/doOrgTreeByUser")
    @ResponseBody
    public List<JwOrgTreeModel> doOrgTreeByUser(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        UserEntity user = (UserEntity)session.getAttribute("user");
        JwOrgEntity jwOrgEntity = orgService.findOne(user.getOrgId());
        List<JwOrgTreeModel> modelList = new ArrayList<JwOrgTreeModel>();
        JwOrgTreeModel orgModel = new JwOrgTreeModel();

        List<JwOrgEntity>clazzList = orgService.findByUser(user);
        if(null!=clazzList && clazzList.size()>0){
            orgModel.setName(jwOrgEntity.getName());
            orgModel.setId(jwOrgEntity.getId());
            orgModel.setText(jwOrgEntity.getName());
            orgModel.setTreeLevel(jwOrgEntity.getTreeLevel());
            orgModel.setParentId(jwOrgEntity.getParent().getId());
            orgModel.setDescription(jwOrgEntity.getDescription());
            orgModel.setType(jwOrgEntity.getType());
            modelList.add(orgModel);
            for(JwOrgEntity jwOrg:clazzList ){
                JwOrgTreeModel orgTreeModelClazz = new JwOrgTreeModel();
                orgTreeModelClazz.setName(jwOrg.getName());
                orgTreeModelClazz.setId(jwOrg.getId());
                orgTreeModelClazz.setText(jwOrg.getName());
                orgTreeModelClazz.setTreeLevel(jwOrg.getTreeLevel());
                orgTreeModelClazz.setParentId(jwOrg.getParent().getId());
                orgTreeModelClazz.setDescription(jwOrg.getDescription());
                orgTreeModelClazz.setType(jwOrg.getType());
                modelList.add(orgTreeModelClazz);
            }
            return modelList;
        }
        if(jwOrgEntity.getTreeLevel()==0){
            modelList=doListTreeOrg();
        }else if(jwOrgEntity.getTreeLevel()==1){
            JwOrgTreeModel orgTreeModel = new JwOrgTreeModel();
            orgTreeModel.setName(jwOrgEntity.getName());
            orgTreeModel.setId(jwOrgEntity.getId());
            orgTreeModel.setText(jwOrgEntity.getName());
            orgTreeModel.setTreeLevel(jwOrgEntity.getTreeLevel());
            orgTreeModel.setDescription(jwOrgEntity.getDescription());
            orgTreeModel.setType(jwOrgEntity.getType());
            orgTreeModel.setState("open");
            List<JwOrgTreeModel> modelList2 = new ArrayList<JwOrgTreeModel>();
            for(JwOrgEntity orgEntity:jwOrgEntity.getOrgChild()){
                JwOrgTreeModel orgTreeModel2 = new JwOrgTreeModel();
                orgTreeModel2.setName(orgEntity.getName());
                orgTreeModel2.setId(orgEntity.getId());
                orgTreeModel2.setText(orgEntity.getName());
                orgTreeModel2.setTreeLevel(orgEntity.getTreeLevel());
                orgTreeModel2.setParentId(orgEntity.getParent().getId());
                orgTreeModel2.setDescription(orgEntity.getDescription());
                orgTreeModel2.setType(orgEntity.getType());
                modelList2.add(orgTreeModel2);
            }
            orgTreeModel.setChildren(modelList2);
            modelList.add(orgTreeModel);
        }
        return modelList;
    }

    /**
     * 根据登录用户返回用户机构及其下级除最低一级的机构树
     * @param request
     * @return
     */
    @RequestMapping("/doListTreeByUser")
    @ResponseBody
    public List<JwOrgTreeModel> doListTreeByUser(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        UserEntity user = (UserEntity)session.getAttribute("user");
        JwOrgEntity jwOrgEntity = orgService.findOne(user.getOrgId());
        List<JwOrgTreeModel> modelList = new ArrayList<JwOrgTreeModel>();
        if(jwOrgEntity.getTreeLevel()==0){
            modelList=doListTreeOrg();
        }else if(jwOrgEntity.getTreeLevel()==1){
            JwOrgTreeModel orgTreeModel = new JwOrgTreeModel();
            orgTreeModel.setName(jwOrgEntity.getName());
            orgTreeModel.setId(jwOrgEntity.getId());
            orgTreeModel.setText(jwOrgEntity.getName());
            orgTreeModel.setTreeLevel(jwOrgEntity.getTreeLevel());
            orgTreeModel.setDescription(jwOrgEntity.getDescription());
            orgTreeModel.setType(jwOrgEntity.getType());
            orgTreeModel.setState("open");
            modelList.add(orgTreeModel);
        }
        return modelList;
    }

    @RequestMapping("/getAdd")
    public ModelAndView getAddOrg() {
        ModelAndView view = new ModelAndView();
        view.setViewName("jw_education/org_add");
        JwOrgTypeEnum[] orgType = JwOrgTypeEnum.values();
        view.addObject("orgType", orgType);
        int treeLevel = 1;
        List<JwOrgEntity> orgList = orgService.findByTreeLevel(0);
        List<JwOrgEntity> orgList2 = orgService.findByTreeLevel(treeLevel);
        orgList.addAll(orgList2);
        view.addObject("orgList", orgList);
        return view;
    }

    @RequestMapping("/doAddOrg")
    @ResponseBody
    public AjaxResult<Boolean> doAddOrg(JwOrgEntity entity) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                if(null!=entity.getParent() && null!=entity.getParent().getId() && !"".equals(entity.getParent().getId())){
                    JwOrgEntity jwOrgEntity = orgService.findOne(entity.getParent().getId());
                    int treeLevel = jwOrgEntity.getTreeLevel();
                    entity.setTreeLevel(treeLevel + 1);
                }else{
                    throw new Exception("请选择上级单位");
                }
                entity.setTreeMax("0000");
                entity.setTreeNumber("0000");
                entity.setIsDel("0");
                entity.setCreateTime(CreateTimeUtils.getCreateTime());
                if(entity.getParent() != null && entity.getParent().getId().equals("2c90ee426565d98e016565db352f0000")) {
                    Long did = ecCardService.doAddOrg(entity);
                    if(null!=did){
                        entity.setDid(did);
                    }
                }

                orgService.save(entity);
                toolService.init();
                return true;
            }
        });
    }

    @RequestMapping("/getUpdateOrg/{id}")
    public ModelAndView getUpdateOrg(@PathVariable final String id) {
        ModelAndView result = new ModelAndView();
        JwOrgEntity entity = orgService.findOne(id);
        result.setViewName("jw_education/org_edit");
        JwOrgTypeEnum[] orgType = JwOrgTypeEnum.values();
        result.addObject("orgType", orgType);
        result.addObject("entity", entity);
        return result;
    }

    @RequestMapping("/doUpdateOrg")
    @ResponseBody
    public AjaxResult<Boolean> doUpdateOrg(final HttpServletRequest request, JwOrgEntity entity) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                JwOrgEntity jwOrgEntity = orgService.findOne(entity.getId());
                JwOrgEntity parentOrgEntity = entity.getParent();
                if(entity.getTreeLevel()>0 && null==entity.getParent()){
                    throw new Exception("请选择上级机构");
                }
                int treeLevel = parentOrgEntity.getTreeLevel();
                jwOrgEntity.setName(entity.getName());
                jwOrgEntity.setType(entity.getType());
                jwOrgEntity.setParent(entity.getParent());
                jwOrgEntity.setFullName(entity.getFullName());
                jwOrgEntity.setDescription(entity.getDescription());
                jwOrgEntity.setTreeLevel(treeLevel+1);
                orgService.save(jwOrgEntity);
                ecCardService.doUpdateOrg(jwOrgEntity);
                toolService.init();
                return true;
            }
        });
    }

    @RequestMapping("/doDeleteOrg")
    @ResponseBody
    public AjaxResult<Boolean> doDeleteOrg(@RequestParam(value = "keys[]") ArrayList<String> keys) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                keys.forEach(id->orgService.delete(id));
                return true;
            }
        });
    }

    private void saveEducationInfo(JwEducationInfoEntity entity, String clobValues) {
        JwEducationClobEntity clobEntity = new JwEducationClobEntity();
        clobEntity.setInfoValue(clobValues);
        entity.setCreateTime(CreateTimeUtils.getCreateTime());
        entity.setClob(clobEntity);
        service.save(entity, clobEntity);
    }
}
